const app = getApp()
Page({
  data: {
    channalList: [
      {
        id: 0,
        status: 1,
        name: '通道1-MIPC3312(P)-X V4.0',
        time: '2025-03-23 14:30:25',
        total: '28天15小时23分',
        period: '2025-03-01 至 2025-03-31',
        count: '30个',
        faultLength: '8小时36分',
        ratio: '1',
        past7dRatio: '1',
        expand: false,
      },
      {
        id: 1,
        status: 0,
        name: '通道2-MIPC3315(P)-X V4.2',
        time: '2025-03-24 09:45:30',
        total: '29天2小时10分',
        period: '2025-03-01 至 2025-03-31',
        count: '28个',
        faultLength: '5小时20分',
        ratio: '0.98',
        past7dRatio: '0.97',
        expand: false,
      },
      {
        id: 2,
        status: 1,
        name: '通道3-MIPC3318(P)-X V5.0',
        time: '2025-03-25 16:20:15',
        total: '30天0小时0分',
        period: '2025-03-01 至 2025-03-31',
        count: '32个',
        faultLength: '12小时10分',
        ratio: '0.96',
        past7dRatio: '0.95',
        expand: false,
      },
      {
        id: 3,
        status: 0,
        name: '通道4-MIPC3320(P)-X V4.5',
        time: '2025-03-26 08:10:05',
        total: '27天8小时0分',
        period: '2025-03-01 至 2025-03-31',
        count: '25个',
        faultLength: '3小时50分',
        ratio: '0.99',
        past7dRatio: '0.98',
        expand: false,
      }
    ]
  },
  onLoad(option) {
    if (!option.id) {
      wx.showToast({
        title: '参数错误',
        icon: 'error'
      })
      wx.navigateBack()
    } else {
      this.setData({
        deviceID: option.id
      })
    }
    this.getDeviceInfo()
  },
  getDeviceInfo() {
    const now = Math.floor(Date.now() / 1000);
    app.axios('tsdevice', {
      op: 'ajaxTsdeviceDetail',
      zaddtime: now,
      opfile: 'tsdevice',
      needzd: 1,
      id: this.data.deviceID
    }).then(res => {
      if (res.code && res.tdlist && res.tdlist.length > 0) {
        res.tdlist.forEach(item => {
          item.totalRecTime = this.timeFormat(item.LoopRecordAllTime)
          item.timeRange = this.rangeFormat(item.recstime, item.recetime)
          const faultTime = item.recetime - item.recstime - item.LoopRecordAllTime
          item.faultTime = this.timeFormat(faultTime)
          item.faultRatio = this.calcRatio(faultTime, item.LoopRecordAllTime)

        })
        this.setData({
          channalList: res.tdlist
        });
      }
    });
  },
  calcRatio(fault, full) {
    let ratio = '--'
    if (full > 0) {
      ratio = ((fault / full) * 100).toFixed(2) + '%'
    }
    return ratio
  },
  timeFormat(totalSeconds) {
    // 转换为数字（避免字符串输入）
    totalSeconds = parseInt(totalSeconds, 10)
    // 计算时分秒
    const hours = Math.floor(totalSeconds / 3600)
    const minutes = Math.floor((totalSeconds % 3600) / 60)
    const seconds = totalSeconds % 60
    // 补零并拼接
    return `${hours}小时${minutes}分${seconds}秒`
  },
  rangeFormat(start, end) {
    let returnStr = ''
    if (start && end) {
      start = app.dateFormatter(start)
      end = app.dateFormatter(end)
      returnStr = `${start} 至 ${end}`
    }
    return returnStr
  },
  toggleExpand(e) {
    const id = e.currentTarget.dataset.id; // 获取当前点击项的id
    const updatedList = this.data.channalList.map(item => {
      if (item.id === id) {
        return { ...item, expand: !item.expand }; // 切换展开状态
      }
      return item;
    });
    this.setData({ channalList: updatedList }); // 更新数据触发渲染
  },
})